package cn.sundat.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.sundat.model.Category;
import cn.sundat.model.News;
import cn.sundat.model.PageBean;

public class NewsDaoImpl extends HibernateDaoSupport implements NewsDao{

	//添加一条消息或者新闻
	public void save(News news) {
		this.getHibernateTemplate().save(news);
	}

	//根据分类查询新闻等
	public List<News> find(DetachedCriteria criteria) {
		List<News>  nlist =  (List<News>) this.getHibernateTemplate().findByCriteria(criteria);
		return nlist;
	}

	//分页查询news
	public PageBean<News> findByPage(Integer currPage, Integer pageSize, DetachedCriteria criteria) {
		PageBean<News> page = new PageBean<News>();
		page.setCurrPage(currPage);
		page.setPageSize(pageSize);
		//先聚合查询
		criteria.setProjection(Projections.rowCount());
		List<Number> list =  (List<Number>) this.getHibernateTemplate().findByCriteria(criteria);
		if(list!=null && list.size()>0) {
			page.setTotalCount(list.get(0).intValue());
		}
		//清空聚合函数查询条件
		criteria.setProjection(null);
		//封装pagebean对象
		List<News> beanlist = (List<News>) this.getHibernateTemplate().findByCriteria(criteria,(currPage-1)*pageSize,pageSize);
		page.setBeanlist(beanlist);
		return page;
	}

	//前台根据分类查询news
	public List<News> findByCategory(Category category) {
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		criteria.addOrder(Order.desc("news_id"));
		criteria.add(Restrictions.eq("category", category));
		criteria.add(Restrictions.eq("news_isshow", 1));
		List<News> beanlist=null;
		if(category.getCate_id()==10||category.getCate_id()==11) {
			 beanlist = (List<News>) this.getHibernateTemplate().findByCriteria(criteria, 0, 6);
		}else if(category.getCate_id()==12){
			beanlist = (List<News>) this.getHibernateTemplate().findByCriteria(criteria, 0, 10);
		}else {
			beanlist = (List<News>) this.getHibernateTemplate().findByCriteria(criteria, 0, 5);
		}
		
		return beanlist;
	}
	
	//后台根据分类查询news
		public List<News> findByAdminCategory(DetachedCriteria criteria) {
			List<News> beanlist = (List<News>) this.getHibernateTemplate().findByCriteria(criteria);
			return beanlist;
		}

	//根据id删除
	public void del(Long news_id) {
		News news = this.getHibernateTemplate().get(News.class, news_id);
		this.getHibernateTemplate().delete(news);
	}
	
	//根据id查询news
	public News findById(Long news_id) {
		News news = this.getHibernateTemplate().get(News.class, news_id);
		return news;
	}

	public void update(News news) {
		this.getHibernateTemplate().update(news);
	}

	public List<News> findImgByCategory(Category c) {
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		criteria.addOrder(Order.desc("news_id"));
		criteria.add(Restrictions.eq("category", c));
		criteria.add(Restrictions.eq("news_isshow", 1));
		criteria.add(Restrictions.isNotNull("img"));
		List<News> beanlist= (List<News>) this.getHibernateTemplate().findByCriteria(criteria, 0, 5);
		return beanlist;
	}

	@Override
	public PageBean<News> findVipNews(Integer currPage, Integer pageSize, DetachedCriteria criteria) {
		return findByPage(currPage, pageSize, criteria);
	}
	
}
